<?php

namespace  App\Http\Controllers\V1;



use App\Helpers\RestResponseFactory;
use App\Helpers\RestUtils;
use App\Helpers\Utils;
use App\Http\Controllers\Controller;

use App\Models\Factory\ComDeviceFactory;
use App\Models\Factory\UserFactory;
use App\Models\Factory\SmsFactory;
use App\Models\Factory\PasswordFactory;
use Illuminate\Http\Request;
class PasswordController extends  Controller
{

    public function testCode(Request $request)
    {
        //接收参数
        $data = $request->input();

        if($data['password'] != $data['repassword']) {
            return RestResponseFactory::ok(RestUtils::getStdObj(), 20001, 20001, RestUtils::getErrorMessage(20001));

        }

//        检查验证码是否正确
        $re = SmsFactory::getSmsRandomFromCache($data['phone'].'_code_kk');
//        if (!($re == $data['code']))
//        {
//            return RestResponseFactory::ok(RestUtils::getStdObj(), RestUtils::getErrorMessage(1204), 1204);
//        }



        //生成该用户的唯一标识token并保存到数据库
        $user = UserFactory::getUserByMobile($data['phone']);

        if(empty($user))
        {
            return RestResponseFactory::ok(RestUtils::getStdObj(),'',1124, RestUtils::getErrorMessage(1124));

        }


        //根据用户的主键生成token
        $token['token'] = md5("kk".time().$user['id']);
        $token['password'] = $data['password'];
        //把用户的唯一标识保存到数据库内
        UserFactory::addUserTokenByMobile($data['phone'],$token);

        //更新用户的最后登录时间
        UserFactory::addUserLassTime($data['phone']);

        return RestResponseFactory::ok(RestUtils::getStdObj());
    }

    public function setPassword(Request $request)
    {
        //接收参数
        $data = $request->input();

        //拿到中间件里赋值的userId
        $user = $request -> get('user');

        if($data['password'] != $user->password){

            return RestResponseFactory::ok(RestUtils::getStdObj(), 20009, 20009, RestUtils::getErrorMessage(20009));

        }

        if($data['newPassword'] != $data['repNewPassword']) {
            return RestResponseFactory::ok(RestUtils::getStdObj(), 20007, 20007, RestUtils::getErrorMessage(20007));
        }

        if($data['newPassword'] == $data['password']){
            return RestResponseFactory::ok(RestUtils::getStdObj(), 20008, 20008, RestUtils::getErrorMessage(20008));

        }

        $user -> password = $data['newPassword'];
        $user ->save();
       return RestResponseFactory::ok(RestUtils::getStdObj(),200);
    }

    public function  setTrPassword(Request $request)
    {

        //接收参数
        $data = $request->input();

        //拿到中间件里赋值的userId
        $user = $request -> get('user');

        if($data['tr_password'] != $user->tr_password){

            return RestResponseFactory::ok(RestUtils::getStdObj(), 20009, 20009, RestUtils::getErrorMessage(20009));

        }

        if($data['newPassword'] != $data['repNewPassword']) {
            return RestResponseFactory::ok(RestUtils::getStdObj(), 20007, 20007, RestUtils::getErrorMessage(20007));
        }

        if($data['newPassword'] == $data['tr_password']){
            return RestResponseFactory::ok(RestUtils::getStdObj(), 20008, 20008, RestUtils::getErrorMessage(20008));

        }

        $user -> tr_password = $data['newPassword'];
        $user ->save();
        return RestResponseFactory::ok(RestUtils::getStdObj(),200);
    }
}